﻿using SL651Center2.x.Enums;
using SL651Center2.x.Messages;
using System.Text;

namespace SL651Center2.x.Send
{
    public class MessageBody
    {
        /// <summary>
        /// 生成查询遥测站时段数据的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="drn">时间步长码。格式：000000</param>
        /// <param name="elements">要素</param>
        /// <returns>正文字节数组</returns>
        /// <exception cref="ArgumentException"></exception>
        public static byte[] BuildTimePeriodDataBody(FrameCode frameCode, DateTime startTime, DateTime endTime, string drn, params ElementCode[] elements)
        {
            if (drn.Length != 6)
            {
                throw new ArgumentException("时间步长码格式错误");
            }
            var dr = Helper.GetSeparateSubString(drn, 2);
            var d = Convert.ToInt32(dr[0]);
            var h = Convert.ToInt32(dr[1]);
            var m = Convert.ToInt32(dr[2]);
            
            /*if (m < 1 || m > 59)
            {
                throw new ArgumentException("时间步长码min位数据错误");
            }else if (h < 1 || h > 23)
            {
                throw new ArgumentException("时间步长码h位数据错误");
            }else if (d < 1 || d > 31)
            {
                throw new ArgumentException("时间步长码d位数据错误");
            }*/

            List<byte> data = new List<byte>();

            //判断报文格式
            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //获取当前时间
                byte[] time = Helper.GetBCDTimeBytes(false);
                //添加发报时间
                data.AddRange(time);

                //起始时间
                byte[] stime = Helper.DateTimeToBCDTimeBytes(startTime, 4, false);
                data.AddRange(stime);

                //添加空格
                data.Add(0x20);

                //结束时间
                byte[] etime = Helper.DateTimeToBCDTimeBytes(endTime, 4, false);
                data.AddRange(etime);

                data.Add(0x20);

                //时间步长码
                string drnx;
                if (d != 0)
                {
                    drnx = "DRD" + dr[0];
                }
                else if (h != 0)
                {
                    drnx = "DRH" + dr[1];
                }
                else if (m != 0)
                {
                    drnx = "DRN" + dr[2];
                }
                else
                {
                    drnx = "DRH00";
                }
                var drnb = Encoding.ASCII.GetBytes(drnx);
                data.AddRange(drnb);

                data.Add(0x20);

                //要素
                foreach (var element in elements)
                {
                    var e = element.ToString();
                    var b = Encoding.ASCII.GetBytes(e);
                    data.AddRange(b);
                    data.Add(0x20);//添加空格
                }

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //获取当前时间
                byte[] time = Helper.GetBCDTimeBytes();
                //添加发报时间
                data.AddRange(time);

                //起始时间
                byte[] stime = Helper.DateTimeToBCDTimeBytes(startTime, 4);
                data.AddRange(stime);

                //结束时间
                byte[] etime = Helper.DateTimeToBCDTimeBytes(endTime, 4);
                data.AddRange(etime);

                //时间步长码
                data.Add(0x04);
                data.Add(0x18);
                byte[] drnb = Helper.HEXStrToByte(drn);
                data.AddRange(drnb);

                //要素(HEX格式只有一个要素)
                data.Add((byte)elements[0]);

                var ddl = ElementInfo.GetValueType(elements[0]);

                //要素的数据定义
                var def = Helper.GetDataDefinitionBytes(ddl);
                data.AddRange(def);

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成查询遥测站指定要素数据的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="elementCodes">要查询的要素</param>
        /// <returns>正文的字节数组</returns>
        public static byte[] BuildElementDataBody(FrameCode frameCode, params ElementCode[] elementCodes)
        {
            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //要素
                foreach (var item in elementCodes)
                {
                    string el = item.ToString();
                    var elb = Encoding.ASCII.GetBytes(el);
                    data.AddRange(elb);
                    //空格
                    data.Add(0x20);
                }
                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //要素
                foreach (var element in elementCodes)
                {
                    //添加要素
                    data.Add((byte)element);

                    if (element == ElementCode.DRP)
                    {
                        var ddl = (12, 1);
                        //要素的数据定义
                        var def = Helper.GetDataDefinitionBytes(ddl);
                        data.AddRange(def);
                    }
                    else if (element.ToString().StartsWith("DRZ"))
                    {
                        var ddl = (24, 1);
                        //要素的数据定义
                        var def = Helper.GetDataDefinitionBytes(ddl);
                        data.AddRange(def);
                    }
                    else
                    {
                        var ddl = ElementInfo.GetValueType(element);
                        //要素的数据定义
                        var def = Helper.GetDataDefinitionBytes(ddl);
                        data.AddRange(def);
                    }
                }
                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成修改遥测站基本配置的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="datas">基本配置参数</param>
        /// <returns>正文字节数组</returns>
        public static byte[] BuildSetBasicConfigBody(FrameCode frameCode, BasicConfigData[] datas)
        {
            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //参数
                foreach (var item in datas)
                {
                    //标识符
                    var el = Helper.ToHEXString((byte)item.Code);
                    var elb = Encoding.ASCII.GetBytes(el);
                    data.AddRange(elb);
                    //空格
                    data.Add(0x20);
                    //数据
                    var db = Encoding.ASCII.GetBytes(item.Data);
                    data.AddRange(db);
                    //空格
                    data.Add(0x20);
                }

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //参数
                foreach (var item in datas)
                {
                    //标识符
                    data.Add((byte)item.Code);
                    //数据长度（字节数）
                    var dl = item.Data.Length / 2;
                    if (item.Data.Length % 2 != 0)
                    { dl++; }
                    //数据定义
                    (int d, decimal per) ddl = (dl, 1);
                    var def = Helper.GetDataDefinitionBytes(ddl);
                    data.AddRange(def);
                    //数据
                    var db = Helper.HEXStrToByte(item.Data);
                    data.AddRange(db);
                }

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成查询遥测站基本配置表的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="basicConfigCode">要查询的基本配置</param>
        /// <returns>正文字节数组</returns>
        public static byte[] BuildQueryBasicConfigBody(FrameCode frameCode, params BasicConfigCode[] basicConfigCode)
        {
            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //参数
                foreach (var element in basicConfigCode)
                {
                    //添加参数
                    var el = Helper.ToHEXString((byte)element);
                    var elb = Encoding.ASCII.GetBytes(el);
                    data.AddRange(elb);
                    //添加空格
                    data.Add(0x20);
                }
                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //要素
                foreach (var item in basicConfigCode)
                {
                    data.Add((byte)item);

                    //数据定义
                    var ddl = BasicConfigInfo.GetValueType(item);
                    var def = Helper.GetDataDefinitionBytes(ddl);
                    data.AddRange(def);
                }

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成设置遥测站运行配置的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="datas">运行配置参数</param>
        /// <returns>正文字节数组</returns>
        public static byte[] BuildSetRunConfigBody(FrameCode frameCode, RunConfigData[] datas)
        {
            List<byte> data = new List<byte>();
            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //参数
                foreach (var item in datas)
                {
                    //标识符
                    var el = Helper.ToHEXString((byte)item.Code);
                    var elb = Encoding.ASCII.GetBytes(el);
                    data.AddRange(elb);
                    //空格
                    data.Add(0x20);
                    //数据
                    var ds = item.Value.ToString();
                    var db = Encoding.ASCII.GetBytes(ds);
                    data.AddRange(db);
                    //空格
                    data.Add(0x20);
                }

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                foreach (var item in datas)
                {
                    //标识符
                    data.Add((byte)item.Code);
                    //数据定义
                    var ddl = RunConfigInfo.GetValueType((byte)item.Code);
                    var def = Helper.GetDataDefinitionBytes(ddl);
                    data.AddRange(def);
                    //数据
                    var num = item.Value * ddl.pre;
                    var snum = num.ToString().PadLeft(ddl.d * 2, '0');
                    var bnum = Helper.HEXStrToByte(snum);
                    data.AddRange(bnum);
                }

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成查询遥测站运行配置的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="runConfigCode">要查询的运行参数</param>
        /// <returns>正文字节数组</returns>
        public static byte[] BuildQueryRunConfigBody(FrameCode frameCode, params RunConfigCode[] runConfigCode)
        {
            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //参数
                foreach (var item in runConfigCode)
                {
                    //添加参数
                    var el = Helper.ToHEXString((byte)item);
                    var elb = Encoding.ASCII.GetBytes(el);
                    data.AddRange(elb);
                    //添加空格
                    data.Add(0x20);
                }

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //要素
                foreach (var item in runConfigCode)
                {
                    //添加要素
                    data.Add((byte)item);

                    //数据定义
                    var ddl = RunConfigInfo.GetValueType((byte)item);
                    var def = Helper.GetDataDefinitionBytes(ddl);
                    data.AddRange(def);
                }

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成初始化固态存储数据的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <returns>正文的字节数组</returns>
        public static byte[] BuildInitializeData(FrameCode frameCode)
        {
            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //添加参数
                var el = Helper.ToHEXString(0x97);
                var elb = Encoding.ASCII.GetBytes(el);
                data.AddRange(elb);

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //添加要素
                data.Add(0x97);
                //数据定义
                data.Add(0x00);

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成恢复遥测站出厂设置的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <returns>正文字节数组</returns>
        public static byte[] BuildFactoryReset(FrameCode frameCode)
        {
            List<byte> data = new List<byte>();
            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //要素
                var el = Helper.ToHEXString(0x98);
                var elb = Encoding.ASCII.GetBytes(el);
                data.AddRange(elb);

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //要素
                data.Add(0x98);
                data.Add(0x00);

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成修改遥测站密码的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="oldPwd">旧密码(十六进制字符串)</param>
        /// <param name="newPwd">新密码(十六进制字符串)</param>
        /// <returns>正文字节数组</returns>
        public static byte[] BuildChangePassWord(FrameCode frameCode, string oldPwd, string newPwd)
        {
            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var hb = Encoding.ASCII.GetBytes(hs);
                data.AddRange(hb);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //密码标识符
                var elb = Encoding.ASCII.GetBytes("03");
                data.AddRange(elb);
                //空格
                data.Add(0x20);
                //旧密码
                var pwdb = Encoding.ASCII.GetBytes(oldPwd);
                data.AddRange(pwdb);
                //空格
                data.Add(0x20);

                //密码标识符
                data.AddRange(elb);
                //空格
                data.Add(0x20);
                //旧密码
                var npwdb = Encoding.ASCII.GetBytes(newPwd);
                data.AddRange(npwdb);
                //空格
                data.Add(0x20);

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //密码标识符
                data.Add(0x03);
                data.Add(0x10);
                //旧密码
                var ob = Helper.HEXStrToByte(oldPwd);
                data.AddRange(ob);

                //密码标识符
                data.Add(0x03);
                data.Add(0x10);
                //新密码
                var nb = Helper.HEXStrToByte(newPwd);
                data.AddRange(nb);
                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成设置遥测站IC卡状态的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="type">IC卡状态。true:开启，false:关闭</param>
        /// <returns>正文字节数组</returns>
        public static byte[] BuildSetIcCardState(FrameCode frameCode, bool type)
        {
            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //标识符
                var elb = Encoding.ASCII.GetBytes("ZT");
                data.AddRange(elb);
                //空格
                data.Add(0x20);

                //IC卡状态数据
                //数据长度四个字节，只能更改IC卡对应位（第９位,0开始）的状态，其余位数据无效
                var state = type ? 0x02 : 0x00;
                var stb = Encoding.ASCII.GetBytes($"0000{state}00");
                data.AddRange(stb);
                data.Add(0x20);

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //标识符
                data.Add(0x45);
                //数据定义
                data.Add(0x20);

                //IC卡状态数据
                //数据长度四个字节，只能更改IC卡对应位的状态，其余位数据无效
                data.Add(0x00);
                data.Add(0x00);
                var state = type ? 0x02 : 0x00;
                data.Add((byte)state);
                data.Add(0x00);

                return data.ToArray();
            }

        }

        /// <summary>
        /// 生成控制水泵/阀门开关命令的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="types">水泵/阀门开关状态数组</param>
        /// <returns>正文的字节数组</returns>
        public static byte[] BuildControlPump(FrameCode frameCode, params bool[] types)
        {
            //字节数
            var ln = types.Length / 8;
            if (types.Length % 8 != 0)
            {
                ln += 1;
            }

            //水泵状态
            char[] chars = new char[types.Length];
            for (int i = 0; i < types.Length; i++)
            {
                chars[i] = types[i] ? '1' : '0';
            }
            //反转数组
            Array.Reverse(chars);
            //得到字节数组
            var str = Convert.ToUInt16(new string(chars), 2);
            var hex = string.Format("{0:X}", str).PadLeft(ln * 2, '0');

            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //字节数
                var hexln = string.Format("{0:X2}", ln);
                var lnb = Encoding.ASCII.GetBytes(hexln);
                data.AddRange(lnb);

                //水泵状态
                var tb = Encoding.ASCII.GetBytes(hex);
                data.AddRange(tb);

                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //字节数
                data.Add((byte)ln);

                //水泵开关状态
                var tb = Helper.HEXStrToByte(hex);
                data.AddRange(tb);

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成控制闸门开关命令的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="types">闸门开关状态数组</param>
        /// <param name="open">闸门开度数据数组</param>
        /// <returns>正文的字节数组</returns>
        public static byte[] BuildControlGate(FrameCode frameCode, bool[] types, params int[] open)
        {
            //字节数
            var ln = types.Length / 8;
            if (types.Length % 8 != 0)
            {
                ln++;
            }

            char[] chars = new char[types.Length];
            //闸门状态
            for (int i = 0; i < types.Length; i++)
            {
                chars[i] = types[i] ? '1' : '0';
            }
            //反转数组
            Array.Reverse(chars);
            //得到字节数组
            var str = Convert.ToUInt16(new string(chars), 2);
            var hex = string.Format("{0:X}", str).PadLeft(ln * 2, '0');

            List<byte> data = new List<byte>();
            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //字节数
                var hexln = string.Format("{0:X2}", ln);
                var lnb = Encoding.ASCII.GetBytes(hexln);
                data.AddRange(lnb);
                //闸门数据
                var tb = Encoding.ASCII.GetBytes(hex);
                data.AddRange(tb);

                data.Add(0x20);
                //闸门开度
                for (int i = 0; i < open.Length; i++)
                {
                    //转换为两字节BCD码
                    var oh = open[i].ToString().PadLeft(4, '0');
                    var ob = Encoding.ASCII.GetBytes(oh);
                    data.AddRange(ob);
                    //空格
                    data.Add(0x20);
                }

                return data.ToArray();

            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //字节数
                data.Add((byte)ln);
                //闸门数据
                var tb = Helper.HEXStrToByte(hex);
                data.AddRange(tb);
                //闸门开度
                for (int i = 0; i < open.Length; i++)
                {
                    //转换为两字节的BCD码
                    var oh = open[i].ToString().PadLeft(4, '0');
                    var ob = Helper.HEXStrToByte(oh);
                    data.AddRange(ob);
                }

                return data.ToArray();
            }
        }

        /// <summary>
        /// 生成控制水量定值命令的正文
        /// </summary>
        /// <param name="frameCode">帧编码格式</param>
        /// <param name="type">定值状态。true:定值投入。false:定值退出</param>
        /// <returns>正文的字节数组</returns>
        public static byte[] BuildControlWaterValue(FrameCode frameCode, bool type)
        {
            List<byte> data = new List<byte>();

            if (frameCode == FrameCode.ASCII)
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Encoding.ASCII.GetBytes(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes(false);
                data.AddRange(time);

                //水量定值数据
                if (type)
                {
                    var d = Encoding.ASCII.GetBytes("FF");
                    data.AddRange(d);
                }
                else
                {
                    var d = Encoding.ASCII.GetBytes("00");
                    data.AddRange(d);
                }
                return data.ToArray();
            }
            else
            {
                //流水号
                var hs = string.Format("{0:X4}", 0);
                var bs = Helper.HEXStrToByte(hs);
                data.AddRange(bs);

                //发报时间
                var time = Helper.GetBCDTimeBytes();
                data.AddRange(time);

                //水量定值数据
                if (type)
                {
                    data.Add(0xFF);
                }
                else
                {
                    data.Add(0x00);
                }

                return data.ToArray();
            }
        }


    }
}
